package com.heqiang.model.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {
	
	private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public final static String formatStr_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public final static String formatStr_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public final static String formatStr_yyyyMMddHH = "yyyy-MM-dd HH";
    public final static String formatStr_yyyyMMdd = "yyyy-MM-dd";
    public final static String formatStr_yyyyMMdd2 = "yyyyMMdd";
    public final static String formatStr_yyyy = "yyyy";
    public final static String formatStr_yyyy年MM月dd日HHmm = "yyyy年MM月dd日 HH:mm";
    public final static String formatStr_MMdd_EE_HHmm = "MM/dd EEEE HH:mm";
    public final static String formatStr_MMdd_HHmm = "MM/dd HH:mm";
    public final static String formatStr_HHmm = "HH:mm";
    public final static String formatStr_MMdd = "MM/dd";
    public final static String formatStr_MM_dd = "MM-dd";
    public final static String formatStr_MMdd2 = "MM月dd日";
    public final static String formatStr_MMddHHmm = "MM-dd HH:mm";
    public final static String formatStr_yyyyMMddHHmmss1 = "yyyy/MM/dd HH:mm:ss";
    public final static String formatStr_yyyyMMddHHmmss2 = "yyyyMMddHHmmss";
    public final static String formatStr_HHMMSS = "HH:mm:ss";
    public final static String formatStr_MM = "MM";
    public final static String formatStr_yyyy年MM月dd日 = "yyyy年MM月dd日";
    public final static String formatStr_yyyyMMddTHHmmssZ = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**
     * 获取今天0点的时间
     *
     * @return
     * @author linjia
     * @datetime 2018年11月2日 下午5:29:38
     */
    public static String getTodayTime() {
        Date now = new Date();
        String nowStr = DateUtils.dateToString(now, DateUtils.formatStr_yyyyMMdd);
        return nowStr + " 00:00:00";
    }


    /**
     * 获取当前时间前几天的日期
     *
     * @param day :
     * @return : java.lang.String
     * @author linjia
     * @date 2019/4/8 14:34
     */
    public static Date getNearDay(Integer day) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.set(Calendar.DATE, ca.get(Calendar.DATE) - day);
        return ca.getTime();
    }

    public static String getNearDay(Integer day,Date date) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.DATE, ca.get(Calendar.DATE) - day);
        return dateToString(ca.getTime(),formatStr_yyyyMMdd);
    }

    // 获取上周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    // 获取上周的结束时间
    public static Date getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }


    // 获取本周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    // 获取本周的结束时间
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }


    // 获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }


    // 获取上月的开始时间
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }

    // 获取上月的结束时间
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getNowYear(), getNowMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getNowYear(), getNowMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }


    // 获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    // 获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

     //获取时间戳：时间、时间类型
    public static Long strToLong(String date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr_yyyyMMddHHmmss);
        Long longTime=0L;
        try {
            Date parse = dateFormat.parse(date);
            longTime=parse.getTime()/1000L;
        } catch (ParseException e) {
        }
        return longTime;
    }


    // 获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    // 获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d)
            calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 	获取两个日期之间的所有日期
     * xiesanchuan
     * @param startTime
     * @param endTime
     * @return
     * 2019年8月24日
     */
    public static List<String> getDateList(String startTime, String endTime) {
        List<String> result = new ArrayList<String>();
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
        	return result;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr_yyyyMMdd);
            Date start_date = sdf.parse(startTime);
            Date end_date = sdf.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
                System.out.println(sdf.format(tempStart.getTime()));
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
        	logger.error("时间格式转换失败,msg:"+e.getMessage(), e);
        }
        Collections.reverse(result);
        return result;
    }


    public static String dateToString(Date date, String toFormatStr) {
        try {
            return new SimpleDateFormat(toFormatStr).format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    /**
     * 0:今日，1昨日，2前日，3本周，4上周，5本月，6上月，7最近一次的周六到周五
     *
     * @param dateTime :
     * @return : java.util.List<java.lang.String>
     * @author linjia
     * @date 2019/8/15 17:01
     */
    public static List<String> countDate(Integer dateTime) {
        List<String> dateTimes = new ArrayList<>();
        if (dateTime == 0) {
            dateTimes.add(DateUtils.dateToString(new Date(), DateUtils.formatStr_yyyyMMdd) + " 00:00:00");
            dateTimes.add(DateUtils.dateToString(new Date(), DateUtils.formatStr_yyyyMMdd) + " 23:59:59");
            return dateTimes;
        } else if (dateTime == 1 || dateTime == 2) {
            dateTimes.add(DateUtils.dateToString(getNearDay(dateTime), DateUtils.formatStr_yyyyMMdd) + " 00:00:00");
            dateTimes.add(DateUtils.dateToString(getNearDay(dateTime), DateUtils.formatStr_yyyyMMdd) + " 23:59:59");
            return dateTimes;
        } else if (dateTime == 3) {
            dateTimes.add(DateUtils.dateToString(getBeginDayOfWeek(), DateUtils.formatStr_yyyyMMddHHmmss));
            dateTimes.add(DateUtils.dateToString(getEndDayOfWeek(), DateUtils.formatStr_yyyyMMddHHmmss));
            return dateTimes;
        } else if (dateTime == 4) {
            dateTimes.add(DateUtils.dateToString(getBeginDayOfLastWeek(), DateUtils.formatStr_yyyyMMddHHmmss));
            dateTimes.add(DateUtils.dateToString(getEndDayOfLastWeek(), DateUtils.formatStr_yyyyMMddHHmmss));
            return dateTimes;
        } else if (dateTime == 5) {
            dateTimes.add(DateUtils.dateToString(getBeginDayOfMonth(), DateUtils.formatStr_yyyyMMddHHmmss));
            dateTimes.add(DateUtils.dateToString(getEndDayOfMonth(), DateUtils.formatStr_yyyyMMddHHmmss));
            return dateTimes;
        }else if (dateTime == 7){
            dateTimes.add( DateUtils.dateToString(getLastSaturday(new Date()),DateUtils.formatStr_yyyyMMddHHmmss));
            dateTimes.add(DateUtils.dateToString(getLastFriday(new Date()),DateUtils.formatStr_yyyyMMddHHmmss));
            return dateTimes;
        }
        dateTimes.add(DateUtils.dateToString(getBeginDayOfLastMonth(), DateUtils.formatStr_yyyyMMddHHmmss));
        dateTimes.add(DateUtils.dateToString(getEndDayOfLastMonth(), DateUtils.formatStr_yyyyMMddHHmmss));
        return dateTimes;
    }



    public static Date getLastSaturday(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayOfWeek==7){
            dayOfWeek=0;
        }
        cal.add(Calendar.DATE, -dayOfWeek-7);
        return getDayStartTime(cal.getTime());
    }
    public static Date getLastFriday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getLastSaturday(date));
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }


    public static String LongToString(long timestamp, String toFormatStr) {
        try {
            Date date = new Date(timestamp);
            SimpleDateFormat sd = new SimpleDateFormat(toFormatStr);
            return sd.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * @Author: zhuangzhihui on 2019/9/6 17:55
     * @param: [date]
     * @return: boolean
     * @Description:判断参数时间是否跟当前时间是同一天
     */
    public static boolean isSameDate(Date date){
        String dateStr = dateToString(date,formatStr_yyyyMMdd);
        String currentStr = dateToString(new Date(),formatStr_yyyyMMdd);
        return dateStr.equals(currentStr);
    }
    public static void main(String[] args) {
        //getDateList("2018-12-26","2018-12-26");
        System.out.println(countDate(7).get(0)+":"+countDate(7).get(1));
    }


    public static Date stringToDate(String dateString) {
        //1、定义转换格式
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr_yyyyMMddHHmmss);
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return date;
    }
    public static Date strToDate(String dateString){
        //1、定义转换格式
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr_yyyyMMdd);
        Date date = null;
        try {
            date = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return date;
    }

    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime) {
        try {
            Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
            String sDateTime = dateToString(dateOld,formatStr_yyyyMMddHHmmss); // 把date类型的时间转换为string
            Date date = stringToDate(sDateTime); // 把String类型转换为Date类型
            return date;
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }

}
